package web.servlet.listthree.service;

import web.servlet.listthree.dao.UserDao;
import web.servlet.listthree.dao.UserDaoImpl;
import web.servlet.listthree.domin.User;

import java.util.List;

/**
 * @Author Administrator
 * @Date 2020/6/18 16:40
 **/
public class UserServiceImpl implements UserService {
    UserDao dao = new UserDaoImpl();

    @Override
    public List<User> findAll() {
        /**
         * 查询
         */
        List<User> users = dao.selectAll();
        return users;
    }

    @Override
    public int add(User user) {
        /**
         * 用户名不能重复
         * 调用dao相关方法
         * if返回null 表示不存在
         * then调用方法
         * else返回-1
         * 表示存在*/
        User user1 = dao.selectByName(user.getName());
        if (user1 == null) {
            int i = dao.insert(user);
            return i;
        } else {
            return -1;
        }

    }

    @Override
    public int update(int id, User user) {
        int i = dao.update(id, user);
        return i;
    }

    @Override
    public User findById(int id) {
        User user = dao.selectById(id);
        return user;
    }

    @Override
    public int delete(int id) {
        int i = dao.delete(id);
        return i;
    }
}
